package com.huaxin.hxmoduleworkorder.feignclients.impl;

import com.huaxin.feignclient.system.SystemClient;
import com.huaxin.feignclient.system.models.Organization;
import com.huaxin.feignclient.system.models.SysDictionary;
import com.huaxin.feignclient.system.models.User;
import com.huaxin.hxmoduleworkorder.dto.ResponseCode;
import com.huaxin.hxmoduleworkorder.feignclients.SysFeignService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SysFeignServiceImpl implements SysFeignService {
    private final SystemClient systemClient;

    /**
     * 获取用户信息
     */
    @Override
    public User getUserInfoById(String userId) {
        com.huaxin.feignclient.models.ResponseResult<User> userInfo = systemClient.getUserInfoById(userId);
        if (userInfo.getCode() != ResponseCode.SUCCESS.getCode() || userInfo.getData() == null) {
            return null;
        }
        return userInfo.getData();
    }

    /**
     * 获取所有管理员ID
     */
    @Override
    public List<String> getAdminIds() {
        com.huaxin.feignclient.models.ResponseResult<List<String>> sysUsers = systemClient.getAllSysUserIds();
        if (sysUsers.getCode() != ResponseCode.SUCCESS.getCode() || sysUsers.getData() == null) {
            return List.of();
        }
        return sysUsers.getData();
    }

    /**
     * 根据组织ID获取组织信息
     */
    @Override
    public Organization getOrganizationById(String id) {
        com.huaxin.feignclient.models.ResponseResult<Organization> organization = systemClient.getOrganizationById(id);
        if (organization.getCode() != ResponseCode.SUCCESS.getCode() || organization.getData() == null) {
            return null;
        }
        return organization.getData();
    }

    /**
     * 获取客户的专属电工
     */
    @Override
    public List<String> getDedicatedElectricianByCustomer(User customer) {
        if (customer == null) {
            return null;
        }
        com.huaxin.feignclient.models.ResponseResult<List<String>> electricians = systemClient.getElectriciansByFamilyIdAndOrgId(customer.getFamilyId(), customer.getOrgId());
        if (electricians.getCode() != ResponseCode.SUCCESS.getCode() || electricians.getData() == null) {
            return List.of();
        }
        return electricians.getData();
    }

    /**
     * 获取组织下所有电工
     */
    @Override
    public List<String> getElectriciansByOrgId(String orgId) {
        com.huaxin.feignclient.models.ResponseResult<List<String>> electricians = systemClient.getElectriciansByOrgId(orgId);
        if (electricians.getCode() != ResponseCode.SUCCESS.getCode() || electricians.getData() == null) {
            return List.of();
        }
        return electricians.getData();
    }

    /**
     * 获取工单自动分配超时小时数（默认24）
     */
    @Override
    public int getAssignTimeoutHours() {
        int assignTimeoutHours = 24;
        try {
            com.huaxin.feignclient.models.ResponseResult<SysDictionary> assignTimeoutHoursResult = systemClient.querySystemDictByKey("assign_timeout_hours");
            assignTimeoutHours = Integer.parseInt(assignTimeoutHoursResult.getData().getDictValue());
        } catch (Exception ignored) {
        }
        return assignTimeoutHours;
    }

    /**
     * 获取工单自动完成超时小时数（默认24）
     */
    @Override
    public int getCompleteTimeoutHours() {
        int completeTimeoutHours = 24;
        try {
            com.huaxin.feignclient.models.ResponseResult<SysDictionary> completeTimeoutHoursResult = systemClient.querySystemDictByKey("complete_timeout_hours");
            completeTimeoutHours = Integer.parseInt(completeTimeoutHoursResult.getData().getDictValue());
        } catch (Exception ignored) {
        }
        return completeTimeoutHours;
    }

    /**
     * 获取维保任务打卡允许的位置误差（米）
     */
    @Override
    public double getLocThresholdMaintenance() {
        double threshold = 50;
        try {
            com.huaxin.feignclient.models.ResponseResult<SysDictionary> locThresholdMaintenanceResult = systemClient.querySystemDictByKey("loc_threshold_maintenance");
            threshold = Double.parseDouble(locThresholdMaintenanceResult.getData().getDictValue());
        } catch (Exception ignored) {
        }
        return threshold;
    }
}
